package com.xiaohao.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohao.news.common.R;
import com.xiaohao.news.dto.NewsDto;
import com.xiaohao.news.entity.*;
import com.xiaohao.news.mapper.NewsMapper;
import com.xiaohao.news.service.CommentService;
import com.xiaohao.news.service.NewsLikeService;
import com.xiaohao.news.service.NewsService;
import com.xiaohao.news.service.NewsTypeService;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Autowired
    private NewsTypeService newsTypeService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private NewsLikeService newsLikeService;

    @Override
    @Transactional
    public void saveWithType(NewsDto newsDto) {
        //保存新闻基本信息，操作news表，执行insert操作
        newsDto.setStatu(1);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        newsDto.setPublishDate(format);

        this.save(newsDto);


        //先获取新闻的id
        Long newsId = newsDto.getId();

        //再获取其中的NewsType对象
        List<NewsType> newsTypes = newsDto.getNewsTypes();

        //遍历NewsType列表并将newId存入
        newsTypes.stream().map((item) -> {
            item.setNewsId(newsId);
            return item;
        }).collect(Collectors.toList());

        //保存新闻分类数据到表news_type中
        newsTypeService.saveBatch(newsTypes);
    }

    @Override
    public Page pageByType(Long typeId, int page, String title) {


        //查询的具体流程如下：
        //1先设置好查询的新闻类型，根据typeId查询news_type表中的newId列表;
        LambdaQueryWrapper<NewsType> newsTypeWrapper = new LambdaQueryWrapper<>();
        newsTypeWrapper.eq(NewsType::getTypeId, typeId).select(NewsType::getNewsId);
        List<Object> newsIds = newsTypeService.getBaseMapper().selectObjs(newsTypeWrapper);

        newsIds.stream().map((item) -> {
            Long.parseLong(item.toString());
            return item;
        }).collect(Collectors.toList()); //将查询出来的结果进行类型转换

        System.out.println(newsIds);
        System.out.println(newsIds.get(1).getClass()); //检查一下


        //2、再设置好分页查询条件
        Page<News> pageInfo = new Page<>(page, 10);
        Page<NewsDto> dtoPageInfo = new Page<>();
        //条件查询器
        LambdaQueryWrapper<News> newsWrapper = new LambdaQueryWrapper<>();
        //设置查询根据
        newsWrapper.in(News::getId, newsIds);
        //选择新闻状态为已审核
        newsWrapper.eq(News::getStatu,1);
        //添加过滤条件，根据title进行like模糊查询
        newsWrapper.like(title != null, News::getTitle, title);
        //添加排序条件，根据发布时间查询
        newsWrapper.orderByDesc(News::getPublishDate);
        //执行分页查询
        this.page(pageInfo, newsWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo, dtoPageInfo, "recodes");//这一步是为了把其他分页信息传给新的Page对象
        List<News> recodes = pageInfo.getRecords();//把page对象里面的实体类提取出来操作

        List<NewsDto> list = recodes.stream().map((item) -> {
            NewsDto newsDto = new NewsDto();
            BeanUtils.copyProperties(item, newsDto);//这里进行第二次对象拷贝，把分页对象里面的实体类搬给dishDto之后，在给赋上categoryName属性

            //获取id以查询其他需要的数据
            Long newsId = item.getId();

            //设置评论数
            LambdaQueryWrapper<Comment> commentWrapper = new LambdaQueryWrapper<>();
            commentWrapper.eq(Comment::getNewsId, newsId);
            int commentCount = commentService.count(commentWrapper);
            System.out.println(commentCount);
            newsDto.setCommentNum(commentCount);

            //设置点赞数
            LambdaQueryWrapper<NewsLike> likeWrapper = new LambdaQueryWrapper<>();
            likeWrapper.eq(NewsLike::getNewsId, newsId);
            int likeCount = commentService.count(commentWrapper);
            System.out.println(likeCount);
            newsDto.setLikeNum(likeCount);

            return newsDto;
        }).collect(Collectors.toList());

        System.out.println(list);

        dtoPageInfo.setRecords(list);


        return dtoPageInfo;
    }

    /**
     * 根据用户id和新闻状态返回新闻列表
     * @param userId
     * @return
     */
    @Override
    public Page pageById(Long userId,int statu) {
        return null;
    }


}
